/*
 *  ECE552H1 F Assignment 1
 *  File Name: mbq2a.c
 *  Author: Mustafa Abbas
 *  Description:
 *      A microbenchmark that executes a fixed number of loads and stores to
 *      test out the hazards due to data loads and stores.
 */

/* defining a parameter to control the benchmark size */
#define BENCHMARK_SIZE 1000

int main () {
    int iter1, iter2;
    int x[BENCHMARK_SIZE], y[BENCHMARK_SIZE];
    int z[BENCHMARK_SIZE][BENCHMARK_SIZE];

    /* initiallize the arrays */
    for (iter1 = 0; iter1 < BENCHMARK_SIZE; iter1++) {
        x[iter1] = iter1;
        y[iter1] = iter1;
    }

    /* load values from the two arrays and put them in the z double array
       this nested loop implementation makes sure that load instructions far
       out number the initialization loop above
       in the assembly, you should see the load instruction issued in a loop
       Here is a snippet of the assembly:
       --------
       $L13:
       lw	$2,16($fp)
       move	$4,$2
       sll	$3,$4,5
       subu	$3,$3,$2
       sll	$4,$3,2
       addu	$4,$4,$2
       sll	$2,$4,5
       addu	$4,$fp,16
       addu	$3,$2,$4
       addu	$2,$3,8008
       lw	$3,20($fp)
       move	$4,$3
       sll	$3,$4,2
       addu	$2,$3,$2
       lw	$3,16($fp)
       move	$4,$3
       sll	$3,$4,2
       addu	$5,$fp,16
       addu	$4,$3,$5
       addu	$3,$4,8
       lw	$4,20($fp)
       move	$5,$4
       sll	$4,$5,2
       addu	$5,$fp,16
       addu	$4,$4,$5
       addu	$5,$4,4008
       lw	$3,0($3)
       lw	$4,0($5)
       addu	$3,$3,$4
       sw	$3,0($2)
       --------
       */

    /* each iteration of this outer loop should do 8 loads
       so in total, we should have a little over 8 million
       loads in the benchmark */
    for (iter1 = 0; iter1 < BENCHMARK_SIZE; iter1++) {
        for (iter2 = 0; iter2 < BENCHMARK_SIZE; iter2++) {
            z[iter1][iter2] = x[iter1] + y[iter2];
        }
    }

    return 0;
}
